<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="8" :lg="11" :md="12" :sm="24">
          <a-form-item label="制单日期">
            <j-date placeholder="请选择开始日期" class="query-group-cust" v-model="startTime_begin"></j-date>
            <span class="query-group-split-cust"></span>
            <j-date placeholder="请选择结束日期" class="query-group-cust" v-model="startTime_end"></j-date>
          </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
              <a @click="handleToggleSearch" style="margin-left: 8px">
                {{ toggleSearchStatus ? '收起' : '展开' }}
                <a-icon :type="toggleSearchStatus ? 'up' : 'down'" />
              </a>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleCount" type="primary" icon="plus">统计</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('原料需求采购')">导出</a-button>
      <!-- 高级查询区域 -->
      <j-super-query
        :fieldList="superFieldList"
        ref="superQueryModal"
        @handleSuperQuery="handleSuperQuery"
      ></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete" />删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down"/></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>



        <span style="float:right;">
          <a-popover title="自定义列" trigger="click" placement="leftBottom">
            <template slot="content">
              <a-checkbox-group @change="onColSettingsChange" v-model="settingColumns" :defaultValue="settingColumns">
                <a-row style="width: 400px">
                  <template v-for="(item,index) in defColumns">
                    <template v-if="item.key!=='rowIndex'&& item.dataIndex!=='action'">
                        <a-col :span="12"><a-checkbox :value="item.dataIndex"><j-ellipsis :value="item.title" :length="10"></j-ellipsis></a-checkbox></a-col>
                    </template>
                  </template>
                </a-row>
              </a-checkbox-group>
            </template>
            <a><a-icon type="setting" />设置</a >
          </a-popover>
        </span>



      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{ x: true }"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        :customRow="setRowStyle"
        class="j-table-force-nowrap1"
        @change="handleTableChange"
      >
        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="warnlevel" slot-scope="text">
          <div v-html="text" :style="computeLevel(text)"></div>
        </template>
        <template slot="imgSlot" slot-scope="text, record">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img
            v-else
            :src="getImgView(text)"
            :preview="record.id"
            height="25px"
            alt=""
            style="max-width:80px;font-size: 12px;font-style: italic;"
          />
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button v-else :ghost="true" type="primary" icon="download" size="small" @click="downloadFile(text)">
            下载
          </a-button>
        </template>
      </a-table>
    </div>
    <material-need-proc-list-modal ref="materialNeedProcList"></material-need-proc-list-modal>
  </a-card>
</template>

<script>
import '@/assets/less/TableExpand.less'
import { mixinDevice } from '@/utils/mixin'
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { getQueryObject } from '@/utils'
import { filterObj } from '@/utils/util'
import MaterialNeedProcListModal from '@views/business/procurement/modules/MaterialNeedProcListModal.vue'
import { getAction } from '@api/manage'

export default {
  name: 'EneFaultRepairList',
  mixins: [JeecgListMixin, mixinDevice],
  components: {
    MaterialNeedProcListModal
  },
  data() {
    return {
      startTime_begin:'',
      startTime_end:'',
      isOpenSettingColumns:true,
      ipagination: {
        current: 1,
        pageSize: 3000,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + "-" + range[1] + " 共" + total + "条"
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0
      },
      description: '故障报修管理页面',
      QueryParams: {},
      // 表头
      columns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '订单编号',
          align: 'center',
          dataIndex: 'schedulingNumber'
        },
        {
          title: '评审表编号',
          align: 'center',
          dataIndex: 'code'
        },
        {
          title: '客户',
          align: 'center',
          dataIndex: 'customerName'
        },
        {
          title: '业务员',
          align: 'center',
          dataIndex: 'salesman_dictText'
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'createBy_dictText'
        },
        {
          title: '制单部门',
          align: 'center',
          dataIndex: 'sysOrgCode_dictText',
        },
        {
          title: '制单时间',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'materialCode',
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName',
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'noneSpec',
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'meUnit_dictText',
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'mainQuantity',
        },
        {
          title: '已生产数量',
          align: 'center',
          dataIndex: 'overNum',
        },
        {
          title: '辅单位',
          align: 'center',
          dataIndex: 'auUnit_dictText',
        },
        {
          title: '辅数量',
          align: 'center',
          dataIndex: 'auxiliaryQuantity',
        },
      ],
      settingColumns: [],

      defColumns: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function(t, r, index) {
            return parseInt(index) + 1
          }
        },
        {
          title: '订单编号',
          align: 'center',
          dataIndex: 'schedulingNumber'
        },
        {
          title: '评审表编号',
          align: 'center',
          dataIndex: 'code'
        },
        {
          title: '客户',
          align: 'center',
          dataIndex: 'customerName'
        },
        {
          title: '业务员',
          align: 'center',
          dataIndex: 'salesman_dictText'
        },
        {
          title: '制单人',
          align: 'center',
          dataIndex: 'createBy_dictText'
        },
        {
          title: '制单部门',
          align: 'center',
          dataIndex: 'sysOrgCode_dictText',
        },
        {
          title: '制单时间',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '物料编码',
          align: 'center',
          dataIndex: 'materialCode',
        },
        {
          title: '物料名称',
          align: 'center',
          dataIndex: 'materialName',
        },
        {
          title: '规格型号',
          align: 'center',
          dataIndex: 'noneSpec',
        },
        {
          title: '单位',
          align: 'center',
          dataIndex: 'meUnit_dictText',
        },
        {
          title: '数量',
          align: 'center',
          dataIndex: 'mainQuantity',
        },
        {
          title: '已生产数量',
          align: 'center',
          dataIndex: 'overNum',
        },
        {
          title: '辅单位',
          align: 'center',
          dataIndex: 'auUnit_dictText',
        },
        {
          title: '辅数量',
          align: 'center',
          dataIndex: 'auxiliaryQuantity',
        },
      ],
      url: {
        list: '/review/crmTradeOrder/needList',
        exportXlsUrl: '/review/crmTradeOrder/exportXls',
        queryCrmTradeOrderGoodsListByMainId:'/review/crmTradeOrder/queryCrmTradeOrderGoodsByMainId'
      },
      dictOptions: {},
      superFieldList: []
    }
  },
  created() {
    this.QueryParams = this.getQueryParams()
    this.loadData();
  },
  computed: {
    importExcelUrl: function() {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    }
  },
  methods: {
    searchReset(noClearQueryParam) {
      if (noClearQueryParam === false) {
        //不清参数
      } else {
        this.queryParam = {};
        this.startTime_end=''
        this.startTime_begin=''
      }
      this.loadData(1);
    },
    loadData(arg, success) {
      if (!this.url.list) {
        this.$message.error("请设置url.list属性!")
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1;
      }
      var params = this.getQueryParams();//查询条件
      params.startTime_begin=this.startTime_begin;
      params.startTime_end=this.startTime_end;
      this.loading = true;
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          //update-begin---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          this.dataSource = res.result.records || res.result;
          this.dataSourceBack=this.dataSource
          if (res.result.total) {
            this.ipagination.total = res.result.total;
          } else {
            this.ipagination.total = 0;
          }
          //update-end---author:zhangyafei    Date:20201118  for：适配不分页的数据列表------------
          typeof success === 'function' ? success(res) : ''
        } else {
          this.$message.warning(res.message)
        }
      }).finally(() => {
        this.loading = false
      })
    },
    searchQuery() {
      if(typeof this.beforeSearch === 'function'){
        let isreturn = this.beforeSearch()
        if(!isreturn){
          return;
        }
      }
      this.loadData(1);
      // 点击查询清空列表选中行
      // https://gitee.com/jeecg/jeecg-boot/issues/I4KTU1
    },
    setRowStyle(record){
      return {
        style: {
          backgroundColor: this.getStatusColor(record.productionStatus),
        },
      };
    },
    getStatusColor(status) {
      const colors = {
        0: 'white',
        1: '#f7dc6f',
        2: '#58D68D',
      };
      return colors[status] || 'white';
    },
    handleCount() {
      const row = this.selectionRows;
      if(row.length===0){
        return this.$message.warning('请选择一条数据')
      }
      this.$nextTick(() => {
        this.$refs.materialNeedProcList.count(row)
      })
    },
    initDictConfig() {},
    getQueryParams(){
      //获取查询条件
      let sqp = {}
      if (this.superQueryParams) {
        sqp['superQueryParams'] = encodeURI(this.superQueryParams)
        sqp['superQueryMatchType'] = this.superQueryMatchType
      }
      var param = Object.assign(sqp, this.queryParam, this.isorter, this.filters);
      param.field = this.getQueryField();
      param.pageNo = this.ipagination.current;
      param.pageSize = this.ipagination.pageSize;
      //已审批
      param.status = 999
      return filterObj(param);
    },
  }
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
